################################
#	John Henderson
#	Gerrymandering Incumbency 
#		(with Brian Hamel and Aaron Goldzimer)
#		April 9, 2017    
#
################################
# code edits that produced by Chen & Rodden (2013), to replicate their 2000 analysis 
#  for competitiveness outcomes                                                 
################################
# calls sims2000.R to load data for plotting

rm(list=ls())    

GetColorHexAndDecimal <- function(color)
{
  c <- col2rgb(color)
  sprintf("#%02X%02X%02X %3d %3d %3d", c[1],c[2],c[3], c[1], c[2], c[3])
}

library(stringr)  
library(foreign)

setwd("~/Dropbox/StateRedistricting/replication/short/")
source('sims2000.R')

#########################
# ad vertical
#########################

sims=s01[ix_ad,] 
elections00=e00[iy_ad,]
elections02=e02[iz_ad,]
     
sims=as.data.frame(rbind(sims,c('MD','SLDL',1,rep(0,dim(sims)[2]-3)),
c('MN','SLDL',1,rep(0,dim(sims)[2]-3)),
c('TX','SLDL',1,rep(0,dim(sims)[2]-3)),
c('NH','SLDL',1,rep(0,dim(sims)[2]-3))))

for(j in 4:ncol(sims)){
	sims[,j]=as.numeric(sims[,j])
}             
  
states_ix=states[c(which(plansAD00=='D'),which(plansAD00=='R'),which(plansAD00=='B'),which(plansAD00=='C'),which(plansAD00=='I'))]
 
propGT=matrix(NA,length(states_ix),2)    

#{
#pdf(width=5,file='~/Dropbox/StateRedistricting/Figures/simFigures/ad_2000-vert.pdf')
#plot(x=-1000, y=-1000, type='n', xlim=c(-.09, .5), ylim=c(.5,20), axes=F, ylab="", xlab="")#, main=paste("Margins of Victory in House Elections\n",state,sep=""))
#axis(1,at=c(0,.1,.2,.3,.4,.5),labels=c(0,.1,.2,.3,.4,.5))

#cnts=0
cnts=20    

#Means and Medians
for (state in states_ix){
	  
	iq=which(state==states_ix)
	cnts=cnts-1
	state.dir <- paste(charts.dir,state,sep="/") 
	temp.sims <- t(sims[sims$st==state,-c(1:3)])
	temp.00 <- elections00[elections00$st==state, -c(1:3)]
	temp.02 <- elections02[elections02$st==state, -c(1:3)]
	#make absolute margin of victory
	temp.sims <- abs(temp.sims)
	temp.00 <- abs(temp.00)
	temp.02 <- abs(temp.02)
      
    t.sim=temp.sims
	for(i in 1:nrow(t.sim)){
		t.sim[i,which(is.na(t.sim[i,]))]=mean(t.sim[i,which(!is.na(t.sim[i,]))],na.rm=T)
		t.sim[i,]=sort(t.sim[i,])
	}
	t.02=(sort(temp.02))
    t.sim=t(t.sim)  
	propGT[iq,2]=mean((t.02>t.sim))

	#Means :: average over simulatons, i.e., counterfactual districtings
	means.sim <- apply(temp.sims, 1, mean,na.rm=T)
	#moments.sim <- quantile(means.sim,prob=c(.025,.975))
	#mean.00 <- mean(temp.00)
	mean.02 <- mean(temp.02)
	#hypothesis-test-like statistics
	mu=mean(means.sim,na.rm=T)
	sds=sd(means.sim,na.rm=T) 
	s.means.sim=((means.sim-mu)/sds)
	s.mean.02=((mean.02-mu)/sds)	
	s.dif=mean(s.mean.02-s.means.sim)  
       
	propGT[iq,1]=mean(means.sim<mean.02)

}         

# lines and legend graphics
#lines(col='grey',lty=2,y=c(.5,.5),x=c(-.0515,.5))  
#lines(col='grey',lty=2,y=c(8.5,8.5),x=c(-.0515,.5))
#lines(col='grey',lty=2,y=c(14.5,14.5),x=c(-.0515,.5)) 
#lines(col='grey',lty=2,y=c(16.5,16.5),x=c(-.0515,.5)) 
#lines(col='grey',lty=2,y=c(19.5,19.5),x=c(-.0515,.5))                                                  

#axis(2,line=-1.5,at=c(.5,8.5,14.5,16.5,19.5),labels=F,tck=-.02)

#text(x=.5-.0105,y=19.,labels='Democratic',cex=.75,adj=c(1,1),srt=0)
#text(x=.5-.0105,y=16.,labels='Republican',cex=.75,adj=c(1,1),srt=0)
#text(x=.5-.0105,y=14.,labels='Bipartisan',cex=.75,adj=c(1,1),srt=0)
#text(x=.5-.0105,y=8.,labels='Court/\nIndependent',cex=.75,adj=c(1,1),srt=0)
#mtext("Mean Margin of Victory",side=1,at=.25,adj=.5,line=3)
#dev.off()
#}     

pl10=plansAD00   

#t.test(propGT[pl10=='I',1],c(propGT[pl10=='B',1],propGT[pl10=='D',1],propGT[pl10=='R',1]))
#t.test(propGT[pl10=='I',2],c(propGT[pl10=='B',2],propGT[pl10=='D',2],propGT[pl10=='R',2]))                                                                                          
t.test(c(propGT[pl10=='I',1],propGT[pl10=='I',2]),c(propGT[pl10=='B',1],propGT[pl10=='D',1],propGT[pl10=='R',1],propGT[pl10=='B',2],propGT[pl10=='D',2],propGT[pl10=='R',2]))
                                                                                           
#t.test(propGT[pl10=='C',1],c(propGT[pl10=='B',1],propGT[pl10=='D',1],propGT[pl10=='R',1]))
#t.test(propGT[pl10=='C',2],c(propGT[pl10=='B',2],propGT[pl10=='D',2],propGT[pl10=='R',2]))
t.test(c(propGT[pl10=='C',1],propGT[pl10=='C',2]),c(propGT[pl10=='B',1],propGT[pl10=='D',1],propGT[pl10=='R',1],propGT[pl10=='B',2],propGT[pl10=='D',2],propGT[pl10=='R',2]))       
 
#t.test(propGT[pl10=='I',1],c(propGT[pl10=='C',1]))
#t.test(propGT[pl10=='I',2],c(propGT[pl10=='C',2]))
t.test(c(propGT[pl10=='I',1],propGT[pl10=='I',2]),c(propGT[pl10=='C',1],propGT[pl10=='C',2]))
                                                                                                                                                                                   
#end
#end  